import router from '@/router'
const lastRoute = [{
    path: '/:pathMatch(.*)',
    component: () => import('@/layout/404.vue'),
    meta: {
        title: '404',
        sidebar: false
    }
}]
export default {
    addKeepAlive(state: any, name: string) {
        if (state.keepAliveArr.indexOf(name) == -1) {
            state.keepAliveArr.push(name)
        }
    },
    removeKeepAlive(state: any, name: string) {
        let index = state.keepAliveArr.indexOf(name)
        if (index != -1) {
            state.keepAliveArr.splice(index, 1)
        }
    },
    removeOtherKeepAlive(state: any, name: string) {
        if (name == 'dashboard') {
            state.keepAliveArr = ['dashboard']
        } else {
            state.keepAliveArr = ['dashboard', name]
        }
    },
    removeLeftKeepAlive(state: any, name: string) {
        if (name != 'dashboard') {
            let index = state.keepAliveArr.indexOf(name)
            let arr = state.keepAliveArr.filter((e, i) => {
                return i >= index
            })
            arr.unshift('dashboard')
            state.keepAliveArr = arr
        }
    },
    removeRightKeepAlive(state: any, name: string) {
        let index = state.keepAliveArr.indexOf(name)
        let arr = state.keepAliveArr.filter((e: any, i: number) => {
            return i <= index
        })
        state.keepAliveArr = arr
    },
    removeAllKeepAlive(state: any, name: string) {
        state.keepAliveArr = ['dashboard']
    },
    setUserInfo(state: any, obj: any) {
        for (var i in obj) {
            localStorage.setItem(i, obj[i])
        }
        state.userInfo = obj
    },
    setMenu(state: any, Menu: any) {
        /**
         * 生成页面菜单
         */
        var newMenu = JSON.parse(JSON.stringify(Menu));
        function formatRouterMenu(arr: any): void {
            arr.forEach((item: any) => {
                if (item.component) {
                    delete item.component
                }

                if (item.children && item.children.length) {
                    formatRouterMenu(item.children)
                }
            })
        }
        formatRouterMenu(newMenu)
        state.menu = newMenu
        localStorage.setItem('menu', JSON.stringify(newMenu))

        /**
         * 生成路由
         */
        let tmp = Menu

        function formatRouter(arr: any): any[] {
            let tmpArr: any[] = []
            let tmpObj = {
                meta: {
                    title: '二级菜单末级集合',
                },
                name: "Layout",
                path: "/tmp",
                component: () => import('@/layout/index.vue'),
                children: [] as any[]
            }
            arr.forEach((item: any) => {
                item.children.forEach((item1: any) => {
                    if (item1.component.name == 'Layout') {
                        tmpArr.push(item1)
                    } else {
                        tmpObj.children.push(item1)
                    }
                })
            })
            tmpArr.push(tmpObj)
            return tmpArr
        }
        var menu = formatRouter(tmp);
        menu.push(...lastRoute)
        menu.forEach(route => {
            router.addRoute(route)
        })
        state.hasGenerateMenu = true
    },
    logout(state: any) {
        state.userInfo.permission = ''
        state.userInfo.username = ''
        state.userInfo.password = ''
        state.userInfo.token = ''
        state.userInfo.menu = ''

        localStorage.removeItem('password')
        localStorage.removeItem('username')
        localStorage.removeItem('token')
        localStorage.removeItem('permission')
        localStorage.removeItem('menu')
        router.push({
            path: "/login"
        });
        state.keepAliveArr = ['dashboard']
    },
}